#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

char* file = "/home/tmp/linux/06-进程替换/mycmd";
char* file2 = "./mycmd";

char* _env[6] = 
{
    "my_env=3455",
    NULL
};
int main()
{
    pid_t id =fork();
    if (id == 0)
    {
        //子进程
        printf("子进程开始执行\n");
     //execl(file2, "mycmd", "-b", NULL);
      char* ary[10] =
      {
          "ls",
          "-a",
          "-l"
      };
      execle(file, "mycmd", "-b", NULL, _env);
     // execvp("/usr/bin/ls", ary);
    // execlp("bash", "bash", "test.sh", NULL);

        exit(11);
    }
    else 
    {
        //父进程
        printf("父进程开始执行\n");
        int status = 0;
        pid_t ret = waitpid(-1, &status, 0);   //阻塞等待,一定是子进程执行完毕,父进程才推出
        if (ret > 0)
        {
            printf("等待成功,返回码: %d\n", WEXITSTATUS(status));
            exit(0);
        }
        else 
        {
            printf("等待失败\n");
        }

    }
    return 0;
}
